Include SDL
Include SDL-image
Include OpenGL

\ Include savepng

: screenshot ( -- surface )
  3 screen-w screen-h * * allocate drop >r
  glPushClientAttrib( GL_CLIENT_pixel_STORE_BIT )
  glPixelStorei( GL_PACK_ROW_LENGTH 0 )
  glPixelStorei( GL_PACK_ALIGNMENT 1 )
  glReadPixels( 0 0 screen-w screen-h GL_RGB GL_UNSIGNED_BYTE r@ )
  glPopClientAttrib
  SDL_CreateRGBSurfaceFrom( r@ screen-w screen-h 24  screen-w 3 *  $ff0000 $ff00 $ff 0 )
  r> free drop ;

: save-screenshot-bmp  ( dest-file-name count -- )
  z$
  screenshot dup >r
  swap SDL_SaveBMP abort" Screenshot save failed somehow."
  r> SDL_FreeSurface ;

{
: save-screenshot-png   ( dest-file-name count -- )
  gl-screenshot dup >r
  -rot save-surface-png
  r> SDL_FreeSurface ;
}
